package com.study.echarts.service.impl;

import com.study.echarts.domain.DeptUserCount;
import com.study.echarts.domain.PositionCount;
import com.study.echarts.domain.ScoreDistribution;
import com.study.echarts.mapper.DeptUserMapper;
import com.study.echarts.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Service
public class DataServiceImpl implements DataService {

    private static final Logger logger = LoggerFactory.getLogger(DataServiceImpl.class);

    @Autowired
    private DeptUserMapper deptUserMapper;

    public List<DeptUserCount> getDeptUserCounts() {
        List<DeptUserCount> deptUserCounts = deptUserMapper.selectDeptUserCounts();
        logger.info("部门用户统计信息: {}", deptUserCounts);
        return deptUserCounts;
    }
    @Override
    public List<PositionCount> getPositionDistribution() {
        return deptUserMapper.selectPositionDistribution().stream()
                .map(record -> {
                    String title = (String) record.get("title");
                    Number count = (Number) record.get("count"); // 兼容 Long、Integer 等
                    return new PositionCount(title, count.intValue()); // 安全转换为 int
                })
                .collect(Collectors.toList());
    }

    @Override
    public List<ScoreDistribution> getScoreDistribution() {
         return deptUserMapper.selectScoreDistribution().stream()
                .map(record -> {
                    String subject = (String) record.get("subject");
                    Number score = (Number) record.get("score"); // 兼容 Long、Integer 等
                    return new ScoreDistribution(subject, score.intValue()); // 安全转换为 int
                })
                .collect(Collectors.toList());
    }
}

